#######生成一个大文件ips.txt,要求120000行，每行随机为172.25.254.0/24段的ip
# import random
# ips=dict()
# fp=open("ips.txt","w",buffering=1)
# for i in range (120000):
#     fp.write("172.25.254.")
#     x=random.randint(0,255)
#     x=str(x)
#     fp.write(x)
#     fp.write("\n")
# ########读取ips.txt文件统计这个文件中ip出现频率前10的ip
# with open("ips.txt") as fp:
#     for ip in fp:
#         ip=ip[:-1]
#         if ip in ips:
#             ips[ip]+=1
#         else:
#             ips[ip]=1
# top10=sorted(ips.items(),key=lambda x:(x[1],x[0]),reverse=True)[:10]
# print(top10,end='')


# #####访客，编写一个程序，提醒用户输入其名字，用户做出响应后，将其名字写入到文件guest.txt中
# fp=open("guest.txt","w",buffering=1)
# while True:
#     name=input("请输入你的名字：")
#     fp.write(name)
#     fp.write("\n")


###############生成100个MAC地址并写入文件，MAC地址前6位（16进制）为01-AF-3B
# import random
# import string
# for i in range(100):
#     with open("mac.txt","a",buffering=1) as fp:
#         fp.write("01-AF-3B-")
#         x=string.hexdigits###################做字符串常量的预初始化字符串，给出16进制字母
#         for i in range(3):
#             n=random.sample(x,2)#可以从指定的序列中，随机的截取指定长度的片断，不作原地修改
#             if i==2:
#
#                 sn=''.join(n)
#                 fp.write(sn)
#             else:
#                 sn="".join(n)
#                 fp.write(sn)
#                 fp.write("-")
#         fp.write("\n")
#########瓦尔登湖
##统计词频


import string
with open("Walden.txt",'r',encoding= 'utf-8') as text:
    words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]###去掉首位的标点符号，并把大写转换为小写
words_index = set(words)#将列表转换为字典
counts_dict = {index:words.count(index) for index in words_index}#定义一个字典，单词为键，频率为值
for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):#以字典中的值为排序的参数
    print('{}：一共有{}次'.format(word,counts_dict[word]))



